package vn.student.controller;

import java.util.Date;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.mylib.Course;
import android.app.Activity;
import android.widget.TextView;

public class ViewEditCourseDetailEditorController {
	Activity act = null;
	String id;
	AStudentDB myDB = null;
	public Course courseInfo = null;
	boolean isCreateNew = false;
	public ViewEditCourseDetailEditorController(Activity act,String id,boolean isCreateNew){
		this.act = act;
		this.id = id;
		this.isCreateNew = isCreateNew;
		myDB = new AStudentDB(act);
		if(!isCreateNew)
			courseInfo = myDB.getCourseDetail(id);
	}
	public ViewEditCourseDetailEditorController(Activity act, String id,boolean isCreateNew, String semesterID) {
		this.act = act;
		this.id = id;
		this.isCreateNew = isCreateNew;
		myDB = new AStudentDB(act);
		if(!isCreateNew)
			courseInfo = myDB.getCourseDetail(id);
		else{
			Date tmp = new Date();
			String dateStr = "";
			String str = "";
			str = String.valueOf(tmp.getYear()+1900);
			dateStr += str + "-";
			str = String.valueOf(tmp.getMonth()+1);
			str = str.length()==2?str:("0"+str);
			dateStr += str + "-";
			str = String.valueOf(tmp.getDate());
			str = str.length()==2?str:("0"+str);
			dateStr += str;
			courseInfo = new Course("","New course",dateStr,dateStr,semesterID);
		}
	}
	public void showCourseDetail(String id) {			
		if(courseInfo==null)
			return;
		TextView tmpLabel = (TextView) act.findViewById(R.id.nameLabel);
		tmpLabel.setText(courseInfo.courseName);
		tmpLabel = (TextView) act.findViewById(R.id.durationLabel);
		tmpLabel.setText(courseInfo.dateStart +" to " + courseInfo.dateEnd);
	}
	public void editCourseName(String name) {
		courseInfo.courseName = name;
	}
	public void editDuration(String dateStartStr, String dateEndStr) {
		// TODO Auto-generated method stub
		courseInfo.dateStart = dateStartStr;
		courseInfo.dateEnd = dateEndStr;
	}
	public boolean updateCourseInfo(){
		if(isCreateNew){
			return myDB.addNewCourse(courseInfo);
		}
		return myDB.updateCourseDetail(courseInfo);
	}
	public void showCourseDetail() {
		if(courseInfo==null)
			return;
		TextView tmpLabel = (TextView) act.findViewById(R.id.nameLabel);
		tmpLabel.setText(courseInfo.courseName);
		tmpLabel = (TextView) act.findViewById(R.id.durationLabel);
		tmpLabel.setText(courseInfo.dateStart +" to " + courseInfo.dateEnd);
	}
}
